/*
 * Copyright 2013, Edmodo, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this work except in compliance with
 * the License.
 * You may obtain a copy of the License in the LICENSE file, or at:
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
 * specific language governing permissions and limitations under the License.
 */

package com.appyvet.materialrangebar;

import ohos.agp.components.Component;
import ohos.agp.utils.Color;
import ohos.agp.render.Canvas;
import ohos.agp.render.LinearShader;
import ohos.agp.render.Paint;
import ohos.agp.render.Shader;
import ohos.agp.utils.Point;

import java.util.ArrayList;

/**
 * Class representing the blue connecting line between the two thumbs.
 */
public class ConnectingLine {
    // Member Variables ////////////////////////////////////////////////////////

    private final int[] colors;
    private final float[] positions;
    private final Paint paint = new Paint();

    private final float mY;

    // Constructor /////////////////////////////////////////////////////////////

    /**
     * Constructor for connecting line
     *
     * @param y                    the y co-ordinate for the line
     * @param connectingLineWeight the weight of the line
     * @param connectingLineColors the color of the line
     */
    public ConnectingLine(float y, float connectingLineWeight,
                            ArrayList<Integer> connectingLineColors) {
        // Need two colors
        if (connectingLineColors.size() == 1) {
            connectingLineColors.add(connectingLineColors.get(0));
        }

        colors = new int[connectingLineColors.size()];
        positions = new float[connectingLineColors.size()];
        for (int index = 0; index < connectingLineColors.size(); index++) {
            colors[index] = connectingLineColors.get(index);

            positions[index] = (float) index / (connectingLineColors.size() - 1);
        }

        paint.setStrokeWidth(connectingLineWeight);
        paint.setStrokeCap(Paint.StrokeCap.ROUND_CAP);
        paint.setAntiAlias(true);

        mY = y;
    }

    private LinearShader getLinearGradient(float startX, float endX, float height) {
            Point startPoint = new Point(startX, height);
            Point endPoint = new Point(endX, height);
            Point[] points = new Point[]{startPoint, endPoint};
        return new LinearShader(points, positions,
                getColors(colors), Shader.TileMode.REPEAT_TILEMODE);
    }

    private Color[] getColors(int[] intColors) {
        Color[] lineColors = new Color[intColors.length];
        for (int i = 0 ; i < intColors.length ; i++) {
            lineColors[i] = new Color(intColors[i]);
        }
        return lineColors;
    }


    /**
     * Draw the connecting line between the two thumbs in rangebar.
     *
     * @param canvas     the Canvas to draw to
     * @param component component view
     * @param leftThumb  the left thumb
     * @param rightThumb the right thumb
     */
    public void draw(Canvas canvas, Component component, PinView leftThumb, PinView rightThumb) {
        paint.setShader(getLinearGradient(0, component.getWidth(), mY),
                Paint.ShaderType.LINEAR_SHADER);

        canvas.drawLine(leftThumb.getX(), mY, rightThumb.getX(), mY, paint);
    }

    /**
     * Draw the connecting line between for single slider.
     *
     * @param canvas     the Canvas to draw to
     * @param component component view
     * @param rightThumb the right thumb
     * @param leftMargin the left margin
     */
    public void draw(Canvas canvas, Component component, float leftMargin, PinView rightThumb) {
        paint.setShader(getLinearGradient(0, component.getWidth(), mY),
                Paint.ShaderType.LINEAR_SHADER);

        canvas.drawLine(leftMargin, mY, rightThumb.getX(), mY, paint);
    }
}
